
    import pandas as pd
    import matplotlib.pyplot as plt
    import numpy as np
    import os
    return np, os, pd, plt

#%%删除'Descriptive Part Name'重复的行

# import pandas as pd
# import matplotlib.pyplot as plt
# import numpy as np

# 前面加_的变量，可以在每个cell中赋值不同？



_f = pd.read_csv('bom_a.csv')
_f = _f.loc[_f['cmate'].notna()]
# 删除字符前面的空格
_f.replace('^\\s+', '', regex=True, inplace=True)
_f.sort_values(by='Descriptive Part Name', inplace=True, ascending=True)
_f.drop_duplicates('Descriptive Part Name', keep='first', inplace=True)
_f.to_csv('bom_b.csv', encoding='utf_8_sig')



#%%将'Descriptive Part Name'中空格开头的单元格，变成空白

    # import pandas as pd
    # import matplotlib.pyplot as plt
    # import numpy as np


_path = 'D:/study_pro/python_code/pandas'
os.chdir(_path)
_csv_f = 'new 2.txt'
_csv_o = 'new 2o.csv'
_f = pd.read_csv(_csv_f)
_f = _f.loc[_f['cmate'].notna()]
_f.replace('^\\s+', '', regex=True, inplace=True)
# 行排序
_f.sort_values(by='Descriptive Part Name', inplace=True, ascending=True)
# 行 过滤
_f = _f.loc[_f['cmate'].apply(lambda a: a != 'elec')]

# 将'Descriptive Part Name'中空格开头的单元格，变成空白
# 使用正则表达式 \\s.*?, 来匹配以一个空白字符开始，后面跟着任意数量的任意字符，直到
# 遇到一个逗号的字符串，并将它们替换为空字符串。
_f.loc[:, 'Descriptive Part Name'].replace('\\s.*?$', '', regex=True, inplace=True)
_f.to_csv(_csv_o, encoding='utf_8_sig')


#%%练习代码
    # import pandas as pd
    # import matplotlib.pyplot as plt
    # import numpy as np


    _path = 'D:/study_pro/python_code/pandas'
    os.chdir(_path)
    _csv_f = 'new 3.txt'
    _csv_o = 'new 3o.csv'
    _f = pd.read_csv(_csv_f)
    _f.sort_values(by='a', inplace=True, ascending=True)
    _f.sort_values(by=['a', 'b'], inplace=True, ascending=True)
    _f.to_csv(_csv_o, encoding='utf_8_sig')


